home *** CD-ROM | disk | FTP | other *** search
Wrap
/* NSDictionary.h Basic dictionary container Copyright 1993, 1994, NeXT, Inc. NeXT, March 1993 */ #import <foundation/NSArray.h> /* This module implement the 'dictionary' concept. Featuring: - dictionaries are always dynamic (resize themselves by doubling when they reach a certain maximum), so that callers never have to think about allocation; - support read only dictionaries; - embeds various conveniences; Generic coding behavior is supplied - it preserves class - immutable dictionaries are distributed bycopy with their elements also bycopy - mutable dictionaries are distributed by reference unless asked bycopy */ /*************** Read Only Abstract Class ***********/ @interface NSDictionary:NSObject <NSCopying, NSMutableCopying> /* NOTES: -copyWithZone: and -copy guarantee an immutable returned value; elements are recursively copied with -copyWithZone: -isEqual: checks that argument is an NSDictionary; If conforming, each item is compared using isEqual:; -hash is such that [x isEqual:y] => [x hash] == [y hash]; */ - (unsigned)count; /* Number of (key,associated object) pairs in the dictionary */ - objectForKey:(NSString *)aKey; /* Get associated object given its key; may return nil */ - (NSEnumerator *)keyEnumerator; /* Returns an enumerator object to cycle through the keys. Note that an object enumerator is also available (see below). Updates should not be done during enumeration To use: NSEnumerator *enumerator = [collection keyEnumerator]; id key; while (key = [enumerator nextObject]) { ... } */ @end @interface NSDictionary (NSExtendedDictionary) - (BOOL)isEqualToDictionary:(NSDictionary *)other; /* Compares 2 dictionaries for exact match; Uses isEqual: for comparison predicate; */ - (NSString *)description; /* Convert to a string, using the "PropertyList" format */ - (NSString *)descriptionWithIndent:(unsigned)level; /* Convert to a string, using the "PropertyList" format; Indent is for human-readibility; level==0 means top level; level==1 => 4 spaces */ - (NSArray *)allKeys; /* returns an array containing all the keys; This snapshots the set of keys; Order is un-defined; */ - (NSArray *)allValues; /* returns an array containing all the associated objects; This snapshots the set of objects; Order is un-defined */ - (NSArray *)allKeysForObject:anObject; /* Returns all keys for which corresponding object isEqual: given argument; Enumerates the dictionary (=> slow, O(N)); Returns nil if none; otherwise an array of count >= 1 */ - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; /* writes a PList string representation */ - (NSEnumerator *)objectEnumerator; /* Returns an enumerator object to cycle through the keys. Note that a key enumerator is available as the primitive. Updates should not be done during enumeration To use: NSEnumerator *enumerator = [collection objectEnumerator]; id object; while (object = [enumerator nextObject]) ... */ @end /*************** Mutable Abstract Class ***********/ @interface NSMutableDictionary: NSDictionary - (void)setObject:anObject forKey:(NSString *)aKey; /* Associate an object for a certain key; If object is nil an exception is raised. -retain is applied to the object inserted in the array. and -release is applied to previous element, if any; The key is always copied by the dictionary */ - (void)removeObjectForKey:(NSString *)aKey; /* Removes key and associated object from the dictionary; Performs -release on previous object if any. */ @end @interface NSMutableDictionary (NSExtendedMutableDictionary) - (void)removeAllObjects; /* Empties the dictionary; Performs -release on each object removed */ - (void)removeObjectsForKeys:(NSArray *)keyArray; /* Removes keys and associated objects from the dictionary */ - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary; /* inserts all the pairs of other; in case of pairs with same keys, other's pairs win */ @end /*************** Immutable Dictionary Creation ***********/ @interface NSDictionary (NSDictionaryCreation) + allocWithZone:(NSZone *)zone; /* Create an uninitialized instance of a concrete dictionary; substitutes a concrete class if called with NSDictionary; +alloc can also be used to that effect */ + dictionary; /* Creates an autoreleased empty dictionary */ + dictionaryWithObjects:(id *)objects forKeys:(NSString **)keys count:(unsigned)count; /* Creates an autoreleased dictionary */ + dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys; /* objects and keys must have the same number of items; All keys must be strings; Creates an autoreleased dictionary */ - initWithObjects:(id *)objects forKeys:(NSString **)keys count:(unsigned)count; /* Designated initializer for immutable dictionaries */ - initWithDictionary:(NSDictionary *)otherDictionary; /* Performs -retain for each item in otherDictionary */ - initWithContentsOfFile:(NSString *)path; /* reads a PList string representation; returns nil on file error or if not a dictionary */ @end /*************** NSMutableDictionary classes ***********/ @interface NSMutableDictionary (NSMutableDictionaryCreation) + allocWithZone:(NSZone *)zone; /* Create an uninitialized instance of a concrete dictionary; substitutes a concrete class if called with NSMutableDictionary; +alloc can also be used to that effect; -dealloc empties dictionary using -removeAllObjects before de-allocating */ + dictionaryWithCapacity:(unsigned)numItems; /* Creates an autoreleased mutable dictionary */ - initWithCapacity:(unsigned)numItems; /* Designated initializer for mutable dictionaries */ @end